package tongwentongshu.com.app.presenter;

import android.app.Activity;
import android.content.Context;
import android.support.v4.util.ArrayMap;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.geocoder.AoiItem;
import com.amap.api.services.geocoder.BusinessArea;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tongwentongshu.com.app.bean.AppItem;
import tongwentongshu.com.app.contract.AgainShareBookContract;
import tongwentongshu.com.app.db.Cache;
import tongwentongshu.com.app.map.LocationOnSuccess;
import tongwentongshu.com.app.map.MapLocation;
import tongwentongshu.com.app.network.ResponseSubscriber;
import tongwentongshu.com.app.update.UpdateFractory;
import tongwentongshu.com.app.utils.ToastUtil;

import static tongwentongshu.com.app.App.CODE;
import static tongwentongshu.com.app.App.NAME;
import static tongwentongshu.com.app.App.POINT;

/**
 * Created by XiongBin on 2017/7/21.
 */

public class AgainShareBookPresenter implements AgainShareBookContract.Presenter, GeocodeSearch.OnGeocodeSearchListener {
    private AgainShareBookContract.View againShareBookView;
    private Map<String, String> mapType;
    private MapLocation mapLocation;//定位对象
    private Context mContext;
    //逆地理编码查询
    private GeocodeSearch geocoderSearch;
    private AMapLocation location;

    public AgainShareBookPresenter(AgainShareBookContract.View againShareBookView) {
        this.againShareBookView = againShareBookView;
    }

    @Override
    public void getBookType() {

    }

    @Override
    public List<PoiItem> getPoiList() {
        return list;
    }

    @Override
    public Map<String, String> getStreet() {
        Map<String, String> map = new HashMap<String, String>();
        if (result.getRegeocodeAddress().getBusinessAreas().size() > 0) {
            BusinessArea b = result.getRegeocodeAddress().getBusinessAreas().get(0);
            map.put(NAME, b.getName());
            map.put(POINT, b.getCenterPoint().getLongitude() + "," + b.getCenterPoint().getLatitude());
            return map;
        } else {
            return getTradingarea();
        }

    }

    @Override
    public Map<String, String> getTradingarea() {
        Map<String, String> map = new HashMap<String, String>();
        if (result.getRegeocodeAddress().getAois().size() > 0) {
            AoiItem a = result.getRegeocodeAddress().getAois().get(0);
            map.put(NAME, a.getAoiName());
            map.put(CODE, a.getAoiId());
            map.put(POINT, a.getAoiCenterPoint().getLongitude() + "," + a.getAoiCenterPoint().getLatitude());
            return map;
        } else {
            PoiItem p = result.getRegeocodeAddress().getPois().get(0);
            map.put(NAME, p.getTitle());
            map.put(CODE, p.getPoiId());
            map.put(POINT, p.getLatLonPoint().getLongitude() + "," + p.getLatLonPoint().getLatitude());
            return map;
        }
    }

    @Override
    public void getAddress(Activity act) {
        mContext = act;
        mapLocation = new MapLocation(act);
        mapLocation.toLocation(locationOnSuccess);
    }

    //定位完毕回调
    public LocationOnSuccess locationOnSuccess = new LocationOnSuccess() {
        @Override
        public void onLocation(AMapLocation location) {
            if (null != location) {
                AgainShareBookPresenter.this.location = location;
                geocoderSearch = new GeocodeSearch(mContext);
                geocoderSearch.setOnGeocodeSearchListener(AgainShareBookPresenter.this);
                RegeocodeQuery query = new RegeocodeQuery(new LatLonPoint(location.getLatitude(), location.getLongitude()), 3000,
                        GeocodeSearch.AMAP);// 第一个参数表示一个Latlng，第二参数表示范围多少米，第三个参数表示是火系坐标系还是GPS原生坐标系
                geocoderSearch.getFromLocationAsyn(query);// 设置异步逆地理编码请求
            }

        }
    };

    private List<PoiItem> list;
    private RegeocodeResult result;

    public AMapLocation getAMapLocation() {
        return location;
    }

    public RegeocodeResult getRegeocodeResult() {
        return result;
    }

    @Override
    public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
        if (rCode == AMapException.CODE_AMAP_SUCCESS) {
            if (result != null && result.getRegeocodeAddress() != null
                    && result.getRegeocodeAddress().getFormatAddress() != null) {
                Log.e("onRegeocodeSearched", result.getRegeocodeAddress().getBusinessAreas().get(0).getName());
                AgainShareBookPresenter.this.result = result;
                list = result.getRegeocodeAddress().getPois();
                againShareBookView.setLocation(list.get(0).getTitle());
                againShareBookView.setPoiItem(list.get(0));
//
            } else {
                ToastUtil.show(mContext, "无");
            }
        } else {
            ToastUtil.showerror(mContext, rCode);
        }
    }

    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {

    }

    private Map<String, String> map;

    @Override
    public void againShare(Map<String, String> map) {
        UpdateFractory.getBuild()
                .name("AgainReleaseBookCall")
                .map(map)
                .build()
                .execute(new ResponseSubscriber<AppItem>() {

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(AppItem item, String msg) {
                        againShareBookView.onSuccess(item);
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }
                });
    }
}
